﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAIN.Abastos.catalogo
{
    public partial class Categorias : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ActualizarDropDownList();
            }
        }

        protected void btnAgregar_Click(object sender, EventArgs e)
        {
            try
            {
                int i=-1;
                BC.Abastos.CategoriasBC controler = new BC.Abastos.CategoriasBC();
                BE.Abastos.Category item = new BE.Abastos.Category();
                item.sCategoryName=this.txtCategoryName.Text;
                item.sDescription = this.txtDescription.Text;
                item.iProductCategoryParentId = int.TryParse(this.ddlCategorias.SelectedValue, out i) ? i : -1;
                controler.Create(item);

                this.wucCategoriiesTree1.Actualizar();

                this.ActualizarDropDownList();
                this.UpdatePanel1.Update();
            }
            catch (Exception ex)
            {
                ExceptionsLogger.WriteLog(ex);
            }
        }
        private void ActualizarDropDownList()
        {
            this.ddlCategorias.Items.Clear();

            List<BE.Abastos.Category> lista = new List<BE.Abastos.Category>();

            lista = new BC.Abastos.CategoriasBC().Select(
                new BE.Abastos.Category()
                {
                    sDescription = string.Empty,
                    sCategoryName = string.Empty,
                    iProductCategoryId = -1,
                    iProductCategoryParentId = -1
                });
            this.ddlCategorias.Items.Add(new ListItem("Ninguna", "-1"));
            foreach (BE.Abastos.Category item in lista)
            {
                this.ddlCategorias.Items.Add(new ListItem(item.sCategoryName, item.iProductCategoryId.ToString()));
            }
        }
        
    }
}